package elements;

import java.util.ArrayList;
import widgets.NodeWidget;

/**
 * The Node class represents a generic node in a Sifu network.
 * The should recognize it's own bandwidth capacities and should
 * also maintain a listing of the other nodes it has a connection with.
 * 
 * @author Matthew Daniels
 */
public class Node {
	
	// What's the IP address of this host?
	private String IP;
	
	// What are the host's upload and download speeds?
	private double upload, download;

	// Who am I connected to?
	private ArrayList<Link> connections = new ArrayList<Link>();

	// What widget visualizes me on canvas?
	private NodeWidget widget;
	
	/**
	 * @param IP - The IP address of the node.
	 * @param ul - The node's maximum uploading bandwidth.
	 * @param dl - The node's maximum downloading bandwidth.
	 */
	public Node (String ip, double ul, double dl) {
		IP = ip;
		upload = ul;
		download = dl;
	}
	
	public ArrayList<Link> getConnections () {
		return connections;
	}
	
	public NodeWidget getWidget() {
		return widget;
	}
	
	public void associateWithWidget(NodeWidget nw, boolean remoteRequest) {
		widget = nw;
	}
	
	private void associateWithWidget(NodeWidget nw) {
		widget = nw;
		nw.associateWithNode(this, true);
	}

	public void addConnection(Link link) {
		connections.add(link);
	}
	
	public double getUploadSpeed() {
		return upload;
	}
	
	public double getDownloadSpeed() {
		return download;
	}
	
	public Node getPartnerNode(Link link) {
		Node[] nodes = link.getNodes();
		if (nodes[0] == this)
			return nodes[1];
		else if (nodes[1] == this)
			return nodes[0];
		else
			System.out.println("Node: getPartnerNode: Link "+link+" does not terminate at "+this+"!");
			return null;
	}
}
